//页面加载的时候就进行查询
$(function(){
	initTree();
	query();
});

//查询报备客户
function query(pageNow) {
	controllerWaitingBar(true);
	//获取项目上下文路径
	rootPath = $('#rootPath').val();
	//获取要查询的客户类型
	var customerType=$("#customerType").val();
	
	//获取当前页的下一页/上一页
	if(!pageNow) {
		pageNow = 1;
	}else if(pageNow == "nextPage") {
		pageNow = $('#pageNow').val();
		var pageCount = $('#pageCount').html();
		//如果当前页是最后一页 则下一页无效
		if(pageNow == pageCount) {
			controllerWaitingBar(false);
			return ;
		}
		pageNow = $('#pageNow').val()*1 + 1;
	}else if(pageNow == "prevPage") {
		pageNow = $('#pageNow').val();
		//如果当前页是第一页 则上一页无效
		if(pageNow == "1") {
			controllerWaitingBar(false);
			return ;
		}
		pageNow = $('#pageNow').val()*1 - 1;
	}else if(pageNow == "lastPage") {
		pageNow = $('#pageCount').html();
	}
	
	var customName = $('#customName').val();
	var startAddTime = $('#startTime').val();
	var endAddTime = $('#endTime').val();
	var phone = $('#phone').val();
	var email = $('#email').val();
	var empId = $('#emp').val();
	var empIdStr = $('#empIdStr').val();
	var regionalId = $('#area').val();
	var nolinkedDay1 = $('#nolinkedDay1').val();
	var nolinkedDay2 = $('#nolinkedDay2').val();
	if(!checkForm()) {
		return ;
	}
	
	var url = rootPath + "/customerManage/queryCustomer";
	$.post(
		url,
		{
			"customName": customName,
			"startAddTime": startAddTime,
			"endAddTime": endAddTime,
			"phone": phone,
			"email": email,
			"empId": empId,
			"empIdStr": empIdStr,
			"regionalId": regionalId,
			"customerType": customerType,
			"nolinkedDay1": nolinkedDay1,
			"nolinkedDay2": nolinkedDay2,
			"pageNow": pageNow
		},
		function(data) {
			var json = eval(data);
			if(5 == json) {
				location.href = rootPath + "/login/loginPage";
				return ;
			}
			var html = "";
			html += '<tr>';
			html += '<th>选择&nbsp;<input type="checkbox" onclick="selectedBox(this)" /></th>';
			/*html += '<th>临时编号</th>';*/
			html += '<th>客户名称</th>';
	        html += '<th>所属人</th>';
	        html += '<th>报备日期</th>';    
	        html += '<th>备注</th>';   
	        html += '<th>未联系天数</th>';   
	        html += '<th>操作</th>';    
	        html += '</tr>'; 
	        if(json.length == 0) {
				html += '<tr><td colspan="8">没有查到相关数据！</td></tr>';
			}else {
				var isResponsible = $('#isResponsible').val();
				var currentdid = $('#currentdid').val();
				for(var i=0; i<json.length; i++) {
					html += '<tr>';
					html += '<td><input type="checkbox"  value="'+ json[i].customId +'"  /></td>';
					/*html += '<td>' + json[i].customId + '</td>';*/
					html += '<td>' + json[i].organizeNameCN + '</td>';
					html += '<td>' + json[i].reportedEmpName + '</td>';
					html += '<td>' + json[i].addTime + '</td>';
					html += '<td>' + json[i].remark + '</td>';
					html += '<td>' + json[i].nolinkedDay + '</td>';
					html += '<td><a href="#" onclick="update(' + "'" + json[i].customId + "'" + ')" >修改</a>  &nbsp; <a href="#" onclick="delReportedCustomer(this)">删除</a>&nbsp&nbsp';
					html += '</td>'; 
					html += '</tr>';
				}
			}
			$('#dataList').html(html);
			
			//显示分页
			if(json.length) {
				var pageNow = json[0].pageNow;
				var pageSize = json[0].pageSize;
				var rowCount = json[0].rowCount;
				var pageCount = json[0].pageCount;
				
				$('#pageNow').val(pageNow);
				$('#pageSize').html("1-" + pageSize);
				$('#rowCount').html(rowCount);
				$('#pageIndex').html(pageNow);
				$('#pageCount').html(pageCount);
			}else {
				$('#pageNow').val("1");
				$('#pageSize').html("1-50");
				$('#rowCount').html("0");
				$('#pageIndex').html("1");
				$('#pageCount').html("1");
			}
			controllerWaitingBar(false);
		}
	);
}

//删除报备客户
function delReportedCustomer(obj) {
	if(obj) {
		var box = $(obj).parent().parent().find(":checkbox");
		box.attr('checked', true);
	}
	
	var idbox = $('#dataList :checkbox');
	var cusIdStr = "" ;
	
	var flag = 0;
	for(var i=0; i< idbox.size(); i++) {
		if(idbox[i].checked) {
			cusIdStr += idbox[i].value + ":";
			
			flag = 1;
		}
	}
	
	if(flag == 0) {
		alert("至少选择一项！");
		return ;
	}
	
	var confirmDel = confirm("确认删除？");
	if(true == confirmDel) {
		
		$.post(
				rootPath + "/customerManage/delReportedCustomer",
				{
					"customerIdStr": cusIdStr
				},
				function(data) {
					location.href = rootPath + "/page/goReportedCustomer";
				}
			);
	}
	
}

//去修改报备客户界面
function update(customId) {
	location.href = rootPath + "/page/goUpdateReportedCustomer?customId=" + customId;
}



